home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / modelers / geomview / source.lha / Geomview / src / bin / geomutil / oogl2rib / oogl2rib.c < prev   
C/C++ Source or Header  |  1993-09-20  |  7KB  |  274 lines

  1. /* Copyright (c) 1993 The Geometry Center; University of Minnesota
  2.    1300 South Second Street Suite 500;  Minneapolis, MN  55454, USA;
  3.    
  4. This file is part of oogl2rib. oogl2rib is free software;
  5. you can redistribute it and/or modify it only under the terms given in
  6. the file COPYING, which you should have received along with this file.
  7. This and other related software may be obtained via anonymous ftp from
  8. geom.umn.edu; email: software@geom.umn.edu. */
  9.  
  10. /* Authors: Scott Wisdom, Tamara Munzner */
  11.  
  12. #include "ooglutil.h"
  13. #include "mg.h"
  14. #include "mgrib.h"
  15. #include "mgribP.h"
  16. #include "mgribtoken.h"
  17. #include "camera.h"
  18. #include "window.h"
  19. #include "appearance.h"
  20. #include "quad.h"
  21. #include "geom.h"
  22. #include "bbox.h"
  23. #include "color.h"
  24. #include <string.h>
  25.  
  26. ColorA white = {1, 1, 1, 1};
  27. Point lightposition = {0, 0, 1};
  28. ColorA ambient = {.2, .2, .2, 1.};
  29. ColorA background = {0, 0, 0, 1}; /* black */
  30.  
  31. #define xPosition 0
  32. #define yPosition 0
  33. #define windowWidth 300
  34. #define windowHeight 300
  35.  
  36. /*
  37.     Switch Descriptions
  38.         default: create a 'world' around the geometry. This will include
  39.         all tokens necessary to position camera, scale and position geometry,
  40.     a provide a singal light illuminating the front of the geometry.
  41.     -g  geometry: geometry only, no world, no world block.
  42.     -b  block: encapsulate geometry-only into a world block. This is the
  43.         configuration to define a RIB file 'clip object' via the Quick
  44.     RenderMan specification.
  45.     -w  define image width;
  46.     -h  define image height;
  47.     -B  background color. Default no background.
  48.     -t  tiff file name. Default "geom.tiff". If "framebuffer", 
  49.         draw to framebuffer, not as tiff file. 
  50. */
  51.  
  52. main(int argc, char **argv)
  53. {
  54.     int i,j;
  55.     float cx,cy,cz,cs,dx,dy,dz;
  56.     HPoint3 min,max;
  57.     Geom *bbox = NULL;
  58.     Transform ObjectTransform;
  59.     LmLighting *lighting;
  60.     LtLight *li[4];
  61.     Camera *cam = NULL;
  62.     WnWindow *win;
  63.     WnPosition position;
  64.     Appearance *ap;
  65.     mgcontext *ctx;
  66.     Geom *ageom;
  67.     float aspect;
  68.     int wwidth;
  69.     int wheight;
  70.     enum _flag {
  71.         geometry,
  72.         minimal,
  73.     world,
  74.     block
  75.     } flag;
  76.     int errflag=0;
  77.     int c, display;
  78.     extern char *optarg;
  79.     extern int optind;
  80.     char *name = "geom.tiff";
  81.     int doback = 0;
  82.     char *infile = "-";
  83.     char *outfile = "-";
  84.     FILE *outfileFD = stdout;
  85.     FILE *infileFD = stdin;
  86.     int framebuffer = 0;
  87.  
  88.     /* defaults */
  89.     flag = world;
  90.     wwidth = windowWidth;
  91.     wheight = windowHeight;
  92.     
  93.     /* process switches */
  94.     while ((c = getopt(argc, argv, "n:w:h:B:gbf")) != EOF)
  95.         switch(c) {
  96.     case 'w':
  97.         wwidth = atoi(optarg);
  98.         break;
  99.     case 'h':
  100.         wheight = atoi(optarg);
  101.         break;
  102.     case 'b':
  103.         flag = block;
  104.         break;
  105.     case 'g':
  106.         flag = geometry;
  107.         break;
  108.     case 'B':
  109.         sscanf(optarg, "%f%*c%f%*c%f%", &(background.r), 
  110.            &(background.g), &(background.b));
  111.         doback = 1;
  112.         break;
  113.     case 'n':
  114.         name = optarg;
  115.         break;
  116.     case 'f':
  117.         framebuffer = 1;
  118.         /* don't change the name if they've set it: 
  119.            check if it's the default */
  120.         if (!strcmp(name, "geom.tiff"))
  121.           name = "geom.rib";
  122.         break;
  123.     default:
  124.         errflag = 1;
  125.         break;
  126.     }
  127.     
  128.         if(optind<argc) {
  129.       infile = argv[optind];
  130.       if (!(infile[0] == '-')) 
  131.         infileFD = fopen(infile, "r");
  132.     }
  133.  
  134.     if((optind+1)<argc) {
  135.         outfile = argv[optind+1];
  136.         if (!(outfile[0] == '-'))
  137.           outfileFD=fopen(outfile, "w+");
  138.         if(!outfileFD) {
  139.             fprintf(stderr,"Unable to open file %s for output.\n",outfile);
  140.             exit(1);
  141.         }
  142.     }
  143.     
  144.         if(errflag) {
  145.         fprintf(stderr,"Usage:\n\
  146. oogl2rib [-n name] [-B r,g,b] [-w width] [-h height] [-fgb] [infile] [outfile]\n\
  147. Convert OOGL file to RenderMan rib format.\n\
  148. Default: read from stdin, write to stdout. Accepts \"-\" as infile/outfile.\n\
  149. -n <name> => name for rendered TIFF file (default \"geom.tiff\")\n\
  150.              or framebuffer window (default \"geom.rib\").\n\
  151. -B r,g,b => background color, each component ranges from 0 to 1. Default none.\n\
  152. -w <width> -h <height> => width/height of rendered frame, in pixels.\n\
  153. -f => .rib file renders to on-screen framebuffer instead of TIFF file.\n\
  154. Default: create complete rib file containing default camera, lights, etc.\n\
  155. -g => only geometry, -b => only Quick Renderman clip object. Ignores -nBwhf.\n");
  156.  
  157.         exit(1);
  158.     }
  159.     
  160.       TmIdentity(ObjectTransform);
  161.  
  162.     mgdevice_RIB();
  163.       
  164.     if(flag==world) {
  165.         cam = CamCreate( CAM_FOV, 60.0, CAM_END );
  166.         
  167.         li[0] = LtCreate(LT_COLOR, &white,
  168.             LT_POSITION, &lightposition,
  169.             LT_INTENSITY, 1.0,
  170.             LT_END);
  171.     
  172.         lighting = LmCreate(LM_AMBIENT, &ambient,
  173.             LM_REPLACELIGHTS, 1,
  174.             LM_END);
  175.         
  176.         lighting->lights = NULL;
  177.         LtAppend(lighting, li[0]);
  178.         ap = ApCreate(AP_DO, APF_FACEDRAW, AP_DO, APF_VECTDRAW,
  179.               AP_LINEWIDTH, 1, 
  180.               AP_MtSet, MT_Kd, 1.0, MT_DIFFUSE, &white, MT_END,
  181.               AP_LGT, lighting,
  182.               AP_SHADING, APF_SMOOTH,
  183.               AP_END);
  184.             
  185.         /* create the virtual window with an arbitrary size */
  186.         position.xmin = 0;
  187.         position.xmax = wwidth-1;
  188.         position.ymin = 0;
  189.         position.ymax = wheight-1;
  190.         win = WnCreate(WN_NAME, "noname", WN_CURPOS, &position, WN_END);
  191.         
  192.         /* update the camera postion and set the propper aspect ratio */
  193.         CamReset(cam);
  194.         WnGet(win, WN_ASPECT, &aspect);
  195.         CamSet(cam, CAM_ASPECT, aspect, CAM_FOV, 60.0, CAM_END);
  196.  
  197.         /* At the moment, mgctxset does not actually change 
  198.            MG_RIBDISPLAY and MG_RIBDISPLAYNAME, so do this during 
  199.            mgctxcreate - TMM
  200.          */
  201.         if(framebuffer) display = MG_RIBFRAME;
  202.         else display = MG_RIBTIFF;
  203.  
  204.         ctx = mgctxcreate(
  205.                 MG_RIBFILE, outfileFD,
  206.             MG_RIBFORMAT, MG_RIBASCII,
  207.             MG_RIBBACKING, MG_RIBNOBG,
  208.             MG_WINDOW, win,    
  209.             MG_CAMERA, cam,
  210.             MG_APPEAR, ap,
  211.             MG_RIBDISPLAY, display,
  212.             MG_RIBDISPLAYNAME, name,
  213.             MG_END );
  214.     
  215.         if(doback) {
  216.         mgctxset(
  217.             MG_RIBBACKING, MG_RIBDOBG,
  218.             MG_BACKGROUND, &background,
  219.             MG_END );
  220.         }
  221.     } else {
  222.         ap = ApCreate(AP_DO, APF_FACEDRAW, AP_DO, APF_VECTDRAW, 
  223.               AP_LINEWIDTH,1,
  224.               AP_SHADING, APF_SMOOTH,  /* Emit normals - slevy */
  225.             AP_END);
  226.         ctx = mgctxcreate(
  227.                 MG_RIBFILE, outfileFD,
  228.             MG_SHOW, 0,        /* overides Format & Display */
  229.              MG_RIBFORMAT, MG_RIBASCII,
  230.             MG_APPEAR, ap,
  231.             MG_END);
  232.     }
  233.  
  234.     ageom = GeomFLoad(infileFD, infile);
  235.  
  236.     if(flag==world) { 
  237.         mgworldbegin();
  238.  
  239.         /* we use the bbox to determain how to place the geometry */
  240.         bbox = GeomBound(ageom, TM_IDENTITY);
  241.         if (bbox != NULL) {
  242.         BBoxMinMax((BBox*)bbox, &min, &max);
  243.         cx = (max.x + min.x) * .5;
  244.         cy = (max.y + min.y) * .5;
  245.         cz = (max.z + min.z) * .5;
  246.         dx = max.x - min.x;
  247.         dy = max.y - min.y;
  248.         dz = max.z - min.z;
  249.         cs = 2.0 / sqrt(dx*dx + dy*dy + dz*dz);
  250.         TmScale(ObjectTransform,cs,cs,cs);
  251.         CtmTranslate(ObjectTransform,-cx,-cy,-cz);
  252.         mgtransform(ObjectTransform);
  253.         }
  254.     }
  255.     
  256.     if(flag==block) {
  257.         mrti(mr_worldbegin, mr_NULL);
  258.     }
  259.     
  260.     GeomDraw(ageom);
  261.     
  262.     if(flag==world) {
  263.        mgworldend();
  264.            mgrib_flushbuffer();
  265.         }
  266.     else {
  267.         if(flag==block) mrti(mr_worldend, mr_NULL);
  268.         mgrib_flushbuffer();
  269.     }
  270.         /* end up with a newline so that rib file can easily be appended to */
  271.         fprintf(outfileFD, "\n");
  272.         fflush(outfileFD);
  273. }
  274.